package com.talent.jiwen.ui.widgets;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.talent.jiwen.R;

import org.w3c.dom.Text;

public class RemindDialog extends Dialog {

    private String content;
    private String negative;
    private String positive;
    private int resId;

    private View.OnClickListener negativeListener;
    private View.OnClickListener positiveListener;

    private boolean oneOption = false;

    public RemindDialog(@NonNull Context context, int resId,String content, String negative, String positive
            , View.OnClickListener negativeListener, View.OnClickListener positiveListener ) {
        super(context);
        this.content = content;
        this.negative = negative;
        this.positive = positive;
        this.negativeListener = negativeListener;
        this.positiveListener = positiveListener;
        this.resId = resId;

        oneOption = false;
    }

    public RemindDialog(@NonNull Context context, int resId,String content
            , String positive,View.OnClickListener positiveListener ) {
        super(context);
        this.content = content;
        this.positive = positive;
        this.positiveListener = positiveListener;
        this.resId = resId;

        oneOption = true;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 背景透明
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);// 不需要标题
        init();
    }

    private void init(){
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.dialog_remind , null);
        setContentView(view);

        TextView contentTv = (TextView)findViewById(R.id.contentTv);
        TextView negativeTv = (TextView)findViewById(R.id.negativeTv);
        TextView positiveTv = (TextView)findViewById(R.id.positiveTv);
        ImageView closeIv = (ImageView)findViewById(R.id.closeIv);
        LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);


        mainLayout.setBackgroundResource(resId);
//        titleTv.setText(title);
        contentTv.setText(content);


        positiveTv.setText(positive);
        positiveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (null != positiveListener){
                    positiveListener.onClick(view);
                }
            }
        });

        if (oneOption){
            negativeTv.setVisibility(View.GONE);
        }else{
            negativeTv.setVisibility(View.VISIBLE);
            negativeTv.setText(negative);
            negativeTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                    if (null != negativeListener){
                        negativeListener.onClick(view);
                    }
                }
            });
        }



        closeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        setCancelable(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = LinearLayout.LayoutParams.MATCH_PARENT;
        lp.height = LinearLayout.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);

    }
}
